from .user_method import Methods

methods = Methods()


class User():
    async def order_info(self, user_id):
        #查询用户所有订单的信息
        if user_id:
            order_sql = 'SELECT * FROM orders WHERE user_id = %s'
            result_info = await methods.select_info(order_sql, (user_id, ))
            if result_info:
                return {
                    'status': 1,
                    'errorCode': None,
                    'errorMsg': None,
                    'resultBody': f'这里是你的订单信息{result_info}'
                }
            else:
                return {
                    'status': -1,
                    'errorCode': 200,
                    'errorMsg': '请检查你的参数',
                    'resultBody': None
                }
        else:
            return {
                'status': -1,
                'errorCode': 2,
                'errorMsg': '请输入你的id号',
                'resultBody': None
            }

    async def merchant_info(self, user_phone):
        #查询商家的所有信息
        merchate_sql = "SELECT * FROM user WHERE user_phone = %s"
        if user_phone:
            result_info = await methods.select_all(merchate_sql,
                                                   (user_phone, ))
            if result_info:
                return {
                    'status': 1,
                    'errorCode': None,
                    'errorMsg': None,
                    'resultBody': f'这里是你的商店信息{result_info}'
                }
            else:
                return {
                    'status': -1,
                    'errorCode': 2,
                    'errorMsg': '你的号码有误',
                    'resultBody': '请输入正确的手机号码',
                }
        else:
            return {
                'status': -1,
                'errorCode': 3,
                'errorMsg': '请检查你的参数',
                'resultBody': None,
            }

    async def submit_order_info(self, user_id, merchat_id, orders):
        #用户下单的操作
        insert_sql = '''
            INSERT INTO orders 
            (user_id,merchat_id,orders)
            VALUES
            (%s,%s,%s)
            '''
        if all([user_id, merchat_id, orders]):
            result_info = await methods.insert_info(
                insert_sql, (user_id, merchat_id, orders))
            if result_info == 'ok':
                return {
                    'status': 1,
                    'errorCode': None,
                    'errorMsg': None,
                    'resultBody': '下单成功',
                }
            else:
                return {
                    'status': -1,
                    'errorCode': 4,
                    'errorMsg': '请检查你的参数',
                    'resultBody': None,
                }
        else:
            return {
                'status': -1,
                'errorCode': 3,
                'errorMsg': '请检查你的参数',
                'resultBody': None,
            }

    async def change_order_info(self, status, user_id):
        #改变用户订单状态的函数
        change_sql = 'UPDATE orders SET status = %s WHERE id = %s '
        if all([status, user_id]):
            result_info = await methods.update_info(change_sql,
                                                    (status, user_id))
            if result_info == 'ok':
                return {
                    'status': 1,
                    'errorCode': None,
                    'errorMsg': None,
                    'resultBody': '改变订单状态成功',
                }
            else:
                return {
                    'status': -1,
                    'errorCode': 2,
                    'errorMsg': '请检查你的参数',
                    'resultBody': None,
                }
        else:
            return {
                'status': -1,
                'errorCode': 3,
                'errorMsg': '请检查你的参数',
                'resultBody': None,
            }
